﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class CMS_Control_CmsRoleTreeView : System.Web.UI.UserControl
{
    Usas.BLL.Cms.cmsArticle ar = new Usas.BLL.Cms.cmsArticle();
    Usas.BLL.Cms.cmsBorder br = new Usas.BLL.Cms.cmsBorder();
    Usas.BLL.Cms.cmsType tp = new Usas.BLL.Cms.cmsType();
    Usas.BLL.Cms.cms_purview_setting cps = new Usas.BLL.Cms.cms_purview_setting();
    Usas.BLL.Cms.AspPager ap = new Usas.BLL.Cms.AspPager();

    Usas.Common.Cms.Role ro = new Usas.Common.Cms.Role();
    Usas.Common.Cms.CurrentInfo cinfo = new Usas.Common.Cms.CurrentInfo();
    string NoneUrl = "javascript:void(0)";
    string Tag = "main";

    #region 外部属性
    private ShowMode _Default = ShowMode.Default;
    private ShowMode _Drop = ShowMode.Drop;
    private ShowMode _Check = ShowMode.Check;

    public enum ShowMode : int
    {
        Default,
        Drop,
        Check,

    }

    public ShowMode Default
    {
        get
        {
            return _Default;
        }
        set
        {
            _Default = value;
        }
    }


    public ShowMode Drop
    {
        get
        {
            return _Drop;
        }
        set
        {
            _Drop = value;
        }
    }

    public ShowMode Check
    {
        get
        {
            return _Check;
        }
        set
        {
            _Check = value;
        }
    }
    #endregion

    public enum RedType : int
    {
        Type,
        Border,
        Article
    }

    private string GetTreeUrl(RedType rt, string id)
    {
        switch (rt)
        {
            case RedType.Type:
                    return "ViewContent.aspx?type=t&id=" + id;
                break;
            case RedType.Border:
                return "ViewContent.aspx?type=b&id=" + id;
                break;
            case RedType.Article:
                return "ViewContent.aspx?type=a&id=" + id;
                break;
        }


        return GetTreeUrlNone(rt, id, false, false);
    }

    private string GetTreeUrlNone(RedType rt, string id, bool view, bool edit)
    {
        return NoneUrl;
    }

    private string GetTreeUrlNoneImage()
    {
        return "~/CMS/images/treeicon/error.jpg";
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 绑定树（先加载Type和Border）
    /// </summary>
    /// <param name="SM">类型</param>
    public void IntiTree(ShowMode SM)
    {

        TreeNode tn = new TreeNode();
        tn.Text = "Hyperion";
        tn.Value = "-1";




        bool edit = false;
        bool check = false;
        bool view = true;
        string father = "";
        string fatherid = "-1";
        if (cinfo.GetSuperAdminGroup())
        {
            edit = true;
            check = true;
            father = "root";
        }
        tn.ImageToolTip = GetTreeUrl(RedType.Type, tn.Value);
        FileTree.Nodes.Add(tn);
        BindType(tn, SM);




    }

    private void BindType(TreeNode tn, ShowMode SM)
    {

        DataSet ds = null;
        DataTable dt = null;
        switch (SM)
        {
            case ShowMode.Default:
                dt = ap.GetTypeTableBySetting(ro.GetSelfSettingRole(), "Del <> 1", 1, -1, "TypeOrder");
                break;
            case ShowMode.Drop:
                dt = ap.GetTypeTableBySetting(ro.GetSelfSettingRole(), " 1=1 ", 1, -1, "TypeOrder");
                break;
        }

        DataRow[] dr = dt.Select("1=1", "TypeOrder desc");

        for (int i = 0; i < dr.Length; i++)
        {
            TreeNode tnn = new TreeNode();
            tnn.Text = dr[i]["TypeName"].ToString();
            tnn.Value = dr[i]["TypeId"].ToString();
            tnn.ImageUrl = "~/CMS/images/treeicon/mappe.gif";

            bool view = Convert.ToInt32(dr[i]["view"]) > 0 ? true : false;
            //tnn.Target = Tag;
            //bool edit = Convert.ToInt32(dr[i]["edit"]) > 0 ? true : false;
            
            //string fh = "";
            //string fhid = "-1";
            //if (!view)
            //{
            //    tnn.ImageUrl = GetTreeUrlNoneImage();
            //}
            //else
            //{
            //    if (edit)
            //    {
            //        if (father == "")
            //        {
            //            fh = "t";
            //            fhid = tnn.Value;
            //        }
            //        else
            //        {
            //            fh = father;
            //            fhid = fatherid;
            //        }
            //    }
            //}



            //tnn.ImageToolTip = GetTreeUrl(RedType.Type, tnn.Value, view, edit, false, "t", fh, fhid);

            tnn.ImageToolTip = GetTreeUrl(RedType.Type, tnn.Value);

            
            tn.ChildNodes.Add(tnn);
            BindBorder(tnn, SM);
           
        }

    }

    private void BindBorder(TreeNode tn, ShowMode SM)
    {
        DataTable dt = null;

        switch (SM)
        {
            case ShowMode.Default:
                dt = ap.GetBorderTableBySetting(ro.GetSelfSettingRole(), "  Del <> 1 and TypeId=" + tn.Value, 1, -1, "BorderOrder");
                break;
            case ShowMode.Drop:
                dt = ap.GetBorderTableBySetting(ro.GetSelfSettingRole(), "  1=1 and TypeId=" + tn.Value, 1, -1, "BorderOrder");
                break;
        }
        if (dt != null && dt.Rows.Count > 0)
        {
            DataRow[] dr = dt.Select("1=1", "BorderOrder desc");
            for (int i = 0; i < dr.Length; i++)
            {
                TreeNode tnn = new TreeNode();
                tnn.Text = dr[i]["BorderName"].ToString();
                tnn.Value = dr[i]["BorderId"].ToString();
                tnn.ImageUrl = "~/CMS/images/treeicon/mappe.gif";

                //tnn.Target = Tag;
                bool view = Convert.ToInt32(dr[i]["view"]) > 0 ? true : false;


                //bool edit = Convert.ToInt32(dr[i]["edit"]) > 0 ? true : false;

                //string fh = "";
                //string fhid = "-1";
                //bool IsCheck = DBNull.Value == dr[i]["IsCheck"] ? false : Convert.ToBoolean(dr[i]["IsCheck"]);

                //if (!view)
                //{
                //    tnn.ImageUrl = GetTreeUrlNoneImage();
                //}
                //else
                //{
                //    if (edit)
                //    {
                //        if (father == "")
                //        {
                //            fh = "b";
                //            fhid = tnn.Value;
                //        }
                //        else
                //        {
                //            fh = father;
                //            fhid = fatherid;
                //        }
                //    }
                //}



                tnn.PopulateOnDemand = true;
                tnn.SelectAction = TreeNodeSelectAction.Select;
                //tnn.ImageToolTip = GetTreeUrl(RedType.Border, tnn.Value, view, edit, IsCheck, "t", fh, fhid);
                tnn.ImageToolTip = GetTreeUrl(RedType.Border, tnn.Value);

                //tnn.ToolTip = SM + "," + fh + "," + fhid + "," + IsCheck.ToString() + "," + dr[i]["TypeId"].ToString() + "," + dr[i]["BorderId"].ToString() + ",0";

                tnn.ToolTip = SM + "," + dr[i]["TypeId"].ToString() + "," + dr[i]["BorderId"].ToString() + ",0";
                tn.ChildNodes.Add(tnn);
                //BinArticle(tnn, SM);
            }
        }
    }

    private void BinArticle(TreeNode tn, ShowMode SM,string typeid,string borderid,string pid)
    {
        DataTable dt = null;

        switch (SM)
        {
            case ShowMode.Default:
                dt = ap.GetArticleTableBySetting(ro.GetSelfSettingRole(), " ((ArtFormat !=1 and PublishStatus != 3) or (ArtFormat =1 and (CanView=0))) and (IsVersion is null or IsVersion=0) and TypeId=" + typeid + " and BorderId=" + borderid + " and ParentArtId=" + pid, 1, -1, "ArtOrder");
                break;
            case ShowMode.Drop:
                dt = ap.GetArticleTableBySetting(ro.GetSelfSettingRole(), " ((ArtFormat !=1 and PublishStatus=3) or (ArtFormat =1 and (CanView=0 or CanView=1))) and (IsVersion is null or IsVersion=0) and TypeId=" + typeid + " and BorderId=" + borderid + " and ParentArtId=" + pid, 1, -1, "ArtOrder");
                break;
        }


        if (dt != null)
        {
            DataRow[] dr = dt.Select("1=1", "ArtOrder desc");
            for (int i = 0; i < dr.Length; i++)
            {
                TreeNode tnnn = new TreeNode();


                tnnn.Text = dr[i]["ArtTopic"].ToString();

                tnnn.Value = dr[i]["ArtId"].ToString();


                tnnn.ImageUrl = "~/CMS/images/treeicon/" + dr[i]["ArtFormat"].ToString() + ".jpg";

                bool view = Convert.ToInt32(dr[i]["view"]) > 0 ? true : false;
                //tnnn.Target = Tag;
                //bool edit = Convert.ToInt32(dr[i]["edit"]) > 0 ? true : false;
               

                //string fh = "";
                //string fhid = "-1";
                //if (!view)
                //{
                //    tnnn.ImageUrl = GetTreeUrlNoneImage();
                //}
                //else
                //{
                //    if (edit)
                //    {
                //        if (father == "")
                //        {
                //            fh = "a";
                //            fhid = tnnn.Value;
                //        }
                //        else
                //        {
                //            fh = father;
                //            fhid = fatherid;
                //        }
                //    }
                //}

                pid = dr[i]["ArtId"].ToString();
                //tnnn.ImageToolTip = GetTreeUrl(RedType.Article, tnnn.Value, view, edit, needCheck, "t", father, fatherid);
                tnnn.ImageToolTip = GetTreeUrl(RedType.Article, tnnn.Value);
                
                tn.ChildNodes.Add(tnnn);


                BinArticle(tnnn, SM,typeid,borderid,pid);
            }
        }
    }

    protected void FileTree_SelectedNodeChanged(object sender, EventArgs e)
    {
        string msg = "parent.document.frames['main'].location.href='" + FileTree.SelectedNode.ImageToolTip +"'";
        Page.ClientScript.RegisterStartupScript(GetType(), "Msg", msg, true);
        return;
        
    }

    protected void FileTree_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {
        TreeNode tnn = new TreeNode();

        if (e.Node != null)
        {
            if (e.Node.ChildNodes.Count <= 0)
            {
                string Toop = e.Node.ToolTip.Trim();
                if (Toop != "")
                {
                    string[] tp = Toop.Split(',');
                    if (ShowMode.Default.ToString() == tp[0])
                    {

                        BinArticle(e.Node, ShowMode.Default, tp[1], tp[2], tp[3]);
                    }
                    else if (ShowMode.Drop.ToString() == tp[0])
                    {
                        BinArticle(e.Node, ShowMode.Drop, tp[1], tp[2], tp[3]);

                    }

                }
            }
        }
    }
}
